<style lang="scss">
  .el-form .el-select {
    display: block;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<template>
  <div>
    <h2 class="clearfix">{{title}}</h2>
    <el-form ref="projectForm" :model="form" label-width="100px" :rules="rules"
             @submit.prevent="onSubmit('projectForm')" onsubmit="return false">
      <el-form-item label="标题" prop="title">
        <el-input type="text" v-model="form.title" placeholder="请输入"></el-input>
      </el-form-item>


      <el-form-item label="附件" prop="photos">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label-width="100px">
        <el-button type="primary" @click="onSubmit('projectForm')" native-type="submit">保存</el-button>
        <el-button @click="doClose">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  let defaultForm = {title: ''};
  export default {
    name: 'editForm',
    props: {
      formItem: {
        type: Object,
        required: true
      },
      title: {
        type: String,
        default: '增加'
      },
      action: {
        type: String,
        default: ''
      }
    },
    mounted() {

    },
    data: function () {

      return {
        imageUrl: '',
        form: Object.assign({}, defaultForm, this.formItem),
        rules: {
          title: [
            {required: true, message: '标题', trigger: 'blur'}
          ]
        },
        doing: false
      };
    },
    watch: {
      formItem: function () {
        this.form = Object.assign({}, defaultForm, this.formItem);

      },
      showRight: function (val) {
        if (!val) {
          this.$refs['projectForm'].resetFields();
        }
      }
    },
    methods: {

      onSubmit: function (formName) {
        let self = this;
        this.$refs[formName].validate((valid) => {
          if (!valid || this.doing) {
            return false;
          }
          this.doing = true;
          let title = '';
        });
      },
      doClose: function () {
        this.$emit('close');
        this.$refs['projectForm'].resetFields();
      },
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    }
  };
</script>
